package com.jeff.tianti.org.dao;

import com.jeff.tianti.common.dao.CustomBaseSqlDaoImpl;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.org.dto.DictionaryDTO;
import com.jeff.tianti.org.entity.Dictionary;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DictionaryDaoImpl extends CustomBaseSqlDaoImpl implements DictionaryCustomDao {
    @Override
    public PageModel<Dictionary> queryDicPage(DictionaryDTO dictionaryDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select t from Dictionary t where 1=1 and t.deleteFlag = 0");
        if (dictionaryDTO != null) {
            if (StringUtils.isNotBlank(dictionaryDTO.getCode())) {
                hql.append(" and t.code like :code ");
                map.put("code", "%" + dictionaryDTO.getCode() + "%");
            }
            if (StringUtils.isNotBlank(dictionaryDTO.getName())) {
                hql.append(" and t.name like :name ");
                map.put("name", "%" + dictionaryDTO.getName() + "%");
            }
            if (dictionaryDTO.getLevel() != null) {
                hql.append(" and t.level = :level ");
                map.put("level", dictionaryDTO.getLevel());
            }
        }
        return this.queryForPageWithParams(hql.toString(), map, dictionaryDTO.getCurrentPage(), dictionaryDTO.getPageSize());
    }

    @Override
    public List<Dictionary> queryDicList(DictionaryDTO dictionaryDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select t from Dictionary t where 1=1 and t.deleteFlag = 0");
        if (dictionaryDTO != null) {
            if (StringUtils.isNotBlank(dictionaryDTO.getCode())) {
                hql.append(" and t.code like :code ");
                if (dictionaryDTO.getCodeLike()){
                    map.put("code", "%" + dictionaryDTO.getCode() + "%");
                }else {
                    map.put("code", dictionaryDTO.getCode());
                }
            }
            if (StringUtils.isNotBlank(dictionaryDTO.getName())) {
                hql.append(" and t.name like :name ");
                map.put("name", "%" + dictionaryDTO.getName() + "%");
            }
            if (dictionaryDTO.getLevel() != null) {
                hql.append(" and t.level = :level ");
                map.put("level", dictionaryDTO.getLevel().toString());
            }

            if (StringUtils.isNotBlank(dictionaryDTO.getPid())) {
                hql.append(" and t.pid = :pid ");
                map.put("pid", dictionaryDTO.getPid());
            }
        }
        return this.queryByMapParams(hql.toString(), map);
    }

    @Override
    public int updateSelectiveById(Dictionary dictionary) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("update Dictionary t set");
        if (dictionary != null) {

            if (StringUtils.isNotBlank(dictionary.getName())) {
                hql.append(" t.name = :name ");
                map.put("name", dictionary.getName());
            }
            if (!StringUtils.isNotBlank(dictionary.getId())) {
                throw new RuntimeException("id 不能为空");
            }

            if (StringUtils.isNotBlank(dictionary.getDeleteFlag())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.deleteFlag = :deleteFlag ");
                } else {
                    hql.append(" t.deleteFlag = :deleteFlag ");
                }
                map.put("deleteFlag", dictionary.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(dictionary.getPid())) {

                if (hql.toString().contains("=")) {
                    hql.append(" ,t.pid = :pid ");
                } else {
                    hql.append(" t.pid = :pid ");
                }
                map.put("pid", dictionary.getPid());
            }

            if (StringUtils.isNotBlank(dictionary.getChannel())) {

                if (hql.toString().contains("=")) {
                    hql.append(" ,t.channel = :channel ");
                } else {
                    hql.append(" t.channel = :channel ");
                }
                map.put("channel", dictionary.getChannel());
            }
            hql.append("where t.id=:id ");
            map.put("id", dictionary.getId());
        }
        return this.update(hql.toString(), map);
    }
}
